<template>
  <div class="room-card" @click="handleCardClick">
    <!-- 房源图片 -->
    <div class="room-image">
      <img :src="room.housePicture" :alt="room.houseTitle" />
      <!-- 收藏按钮（可选） -->
      <button v-if="showFavorite" class="fav-btn" :class="{ active: isFav }" @click.stop="toggleFavorite">
        {{ isFav ? '已收藏' : '收藏' }}
      </button>
      <!-- 标签 -->
      <div class="room-tags" v-if="room.tags && room.tags.length">
        <span 
          v-for="tag in room.tags.slice(0, 2)" 
          :key="tag.name"
          class="room-tag"
        >
          {{ tag.name }}
        </span>
      </div>
    </div>
    
    <!-- 房源信息 -->
    <div class="room-info">
      <!-- 标题 -->
      <h4 class="room-title">{{ room.houseTitle }}</h4>
      
      <!-- 地址 -->
      <div class="room-address">
        <el-icon class="location-icon">
          <Location />
        </el-icon>
        <span class="address-text">{{ room.address }}</span>
      </div>
      
      <!-- 价格和面积 -->
      <div class="room-details">
        <div class="price-info">
          <span class="price">¥{{ room.rentPriceListing }}</span>
          <span class="price-unit">/{{ room.rentPriceUnit }}</span>
        </div>
        <div class="area-info">
          <span class="area">{{ room.rentArea }}㎡</span>
        </div>
      </div>
      
      <!-- 操作按钮 -->
      <div class="room-actions">
        <button 
          class="action-btn call-btn" 
          @click.stop="handleCall"
        >
          电话
        </button>
        <button 
          class="action-btn visit-btn" 
          @click.stop="handleVisit"
        >
          实地看
        </button>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ElMessage } from 'element-plus'
import { Location } from '@element-plus/icons-vue'

const props = defineProps({
  room: {
    type: Object,
    required: true
  },
  showFavorite: {
    type: Boolean,
    default: false
  },
  initialFavorited: {
    type: Boolean,
    default: false
  }
})

const emit = defineEmits(['click','favorite-change'])

const handleCardClick = () => {
  emit('click', props.room)
}

const handleCall = () => {
  ElMessage.info(`拨打电话查看房源：${props.room.houseTitle}`)
}

const handleVisit = () => {
  ElMessage.info(`预约实地看房：${props.room.houseTitle}`)
}

// 收藏交互
import { ref, watchEffect } from 'vue'
const isFav = ref(props.initialFavorited)

watchEffect(() => {
  isFav.value = props.initialFavorited
})

const toggleFavorite = () => {
  isFav.value = !isFav.value
  emit('favorite-change', { id: props.room.id, favorited: isFav.value })
}
</script>

<style scoped>
.room-card {
  background-color: #fff;
  border-radius: 12px;
  overflow: hidden;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  cursor: pointer;
  transition: all 0.3s ease;
  border: 1px solid #f0f0f0;
}

.room-card:hover {
  transform: translateY(-2px);
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.15);
  border-color: #e0e0e0;
}

.room-image {
  position: relative;
  width: 100%;
  height: 120px;
  overflow: hidden;
}

.room-image img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  transition: transform 0.3s ease;
}

.room-card:hover .room-image img {
  transform: scale(1.05);
}

.room-tags {
  position: absolute;
  top: 8px;
  left: 8px;
  display: flex;
  gap: 4px;
}

.fav-btn {
  position: absolute;
  top: 8px;
  right: 8px;
  padding: 4px 8px;
  font-size: 12px;
  border-radius: 12px;
  border: 1px solid #e0e0e0;
  background: rgba(255,255,255,0.9);
  color: #333;
  cursor: pointer;
}

.fav-btn.active {
  background: #ffefe6;
  border-color: #ff7a45;
  color: #d4380d;
}

.room-tag {
  background-color: rgba(64, 158, 255, 0.9);
  color: #fff;
  font-size: 10px;
  padding: 2px 6px;
  border-radius: 4px;
  backdrop-filter: blur(4px);
}

.room-info {
  padding: 12px;
}

.room-title {
  font-size: 14px;
  font-weight: 600;
  color: #333;
  margin: 0 0 8px 0;
  line-height: 1.3;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
  text-overflow: ellipsis;
  min-height: 36px;
}

.room-address {
  display: flex;
  align-items: center;
  margin-bottom: 8px;
}

.location-icon {
  font-size: 12px;
  color: #999;
  margin-right: 4px;
}

.address-text {
  font-size: 12px;
  color: #666;
  line-height: 1.2;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.room-details {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 12px;
}

.price-info {
  display: flex;
  align-items: baseline;
}

.price {
  font-size: 16px;
  font-weight: 600;
  color: #e74c3c;
}

.price-unit {
  font-size: 12px;
  color: #999;
  margin-left: 2px;
}

.area-info {
  font-size: 12px;
  color: #666;
}

.room-actions {
  display: flex;
  gap: 8px;
}

.action-btn {
  flex: 1;
  padding: 6px 0;
  border: none;
  border-radius: 6px;
  font-size: 12px;
  cursor: pointer;
  transition: all 0.3s ease;
}

.call-btn {
  background-color: #f8f9fa;
  color: #333;
  border: 1px solid #e9ecef;
}

.call-btn:hover {
  background-color: #e9ecef;
  border-color: #ddd;
}

.visit-btn {
  background-color: #409eff;
  color: #fff;
}

.visit-btn:hover {
  background-color: #337ecc;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .room-image {
    height: 100px;
  }
  
  .room-info {
    padding: 10px;
  }
  
  .room-title {
    font-size: 13px;
    min-height: 32px;
  }
  
  .address-text {
    font-size: 11px;
  }
  
  .price {
    font-size: 14px;
  }
  
  .price-unit,
  .area-info {
    font-size: 11px;
  }
  
  .action-btn {
    padding: 5px 0;
    font-size: 11px;
  }
  
  .room-tag {
    font-size: 9px;
    padding: 1px 4px;
  }
}

/* 卡片加载动画 */
@keyframes fadeInUp {
  from {
    opacity: 0;
    transform: translateY(20px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.room-card {
  animation: fadeInUp 0.5s ease;
}
</style>
